package org.kabanyo.components {
	import flash.events.MouseEvent;

	import mx.collections.IList;
	import mx.core.IFactory;

	import org.kabanyo.components.supportClasses.ToolTipRenderer;
	import org.kabanyo.events.MapEvent;

	import spark.components.Group;
	import spark.components.SkinnableContainer;

	[Event(name="mapSelected", type="org.kabanyo.events.MapEvent")]

	/**
	 * Map of Indonesia with province selector.
	 *
	 * @author Desky Natalio
	 *
	 */

	public class IndonesiaProvinceMap extends SkinnableContainer {
		//Skin Part
		[SkinPart(required="true")]
		public var mapGroup:Group;
		[SkinPart(required="true")]
		public var acehButton:MapButton;
		[SkinPart(required="true")]
		public var sumutButton:MapButton;
		[SkinPart(required="true")]
		public var sumbarButton:MapButton;
		[SkinPart(required="true")]
		public var riauButton:MapButton;
		[SkinPart(required="true")]
		public var kepriButton:MapButton;
		[SkinPart(required="true")]
		public var bengkuluButton:MapButton;
		[SkinPart(required="true")]
		public var jambiButton:MapButton;
		[SkinPart(required="true")]
		public var sumselButton:MapButton;
		[SkinPart(required="true")]
		public var babelButton:MapButton;
		[SkinPart(required="true")]
		public var lampungButton:MapButton;
		[SkinPart(required="true")]
		public var bantenButton:MapButton;
		[SkinPart(required="true")]
		public var dkiButton:MapButton;
		[SkinPart(required="true")]
		public var jabarButton:MapButton;
		[SkinPart(required="true")]
		public var jatengButton:MapButton;
		[SkinPart(required="true")]
		public var diyButton:MapButton;
		[SkinPart(required="true")]
		public var jatimButton:MapButton;
		[SkinPart(required="true")]
		public var baliButton:MapButton;
		[SkinPart(required="true")]
		public var ntbButton:MapButton;
		[SkinPart(required="true")]
		public var nttButton:MapButton;
		[SkinPart(required="true")]
		public var kalbarButton:MapButton;
		[SkinPart(required="true")]
		public var kaltengButton:MapButton;
		[SkinPart(required="true")]
		public var kaltimButton:MapButton;
		[SkinPart(required="true")]
		public var kalselButton:MapButton;
		[SkinPart(required="true")]
		public var sulutButton:MapButton;
		[SkinPart(required="true")]
		public var gorontaloButton:MapButton;
		[SkinPart(required="true")]
		public var sultengButton:MapButton;
		[SkinPart(required="true")]
		public var sulbarButton:MapButton;
		[SkinPart(required="true")]
		public var sultraButton:MapButton;
		[SkinPart(required="true")]
		public var sulselButton:MapButton;
		[SkinPart(required="true")]
		public var malutButton:MapButton;
		[SkinPart(required="true")]
		public var malukuButton:MapButton;
		[SkinPart(required="true")]
		public var papbarButton:MapButton;
		[SkinPart(required="true")]
		public var papuaButton:MapButton;

		private var _selectedMap:MapButton;

		[Bindable]
		public function get selectedMap():MapButton {
			return _selectedMap;
		}

		public function set selectedMap(value:MapButton):void {
			_selectedMap=value;
		}

		private var _toolTipRenderer:IFactory;

		[Bindable]
		public function get toolTipRenderer():IFactory {
			return _toolTipRenderer;
		}

		public function set toolTipRenderer(value:IFactory):void {
			_toolTipRenderer=value;
		}

		private var _colorIndicators:Array;

		[Bindable]
		/**
		 * Set button color by indicated value.
		 * @return
		 *
		 */
		[ArrayElementType("org.kabanyo.components.IColorIndicator")]
		public function get colorIndicators():Array {
			return _colorIndicators;
		}

		/**
		 * @private
		 */
		public function set colorIndicators(value:Array):void {
			_colorIndicators=value;
		}

		private var _mapData:IList;

		[Bindable]
		public function get mapData():IList {
			return _mapData;
		}

		public function set mapData(value:IList):void {
			_mapData=value;
			trace("Set All Map Data from map data setting.");
			if (mapData) {
				setAllData();
			}
		}

		private var _mapValueField:String="mapValue";

		[Bindable]
		public function get mapValueField():String {
			return _mapValueField;
		}

		public function set mapValueField(value:String):void {
			_mapValueField=value;
		}

		private var _toolTipDataField:String="toolTipData";

		[Bindable]
		public function get toolTipDataField():String {
			return _toolTipDataField;
		}

		public function set toolTipDataField(value:String):void {
			_toolTipDataField=value;
		}

		private var _mapButtonNameField:String="mapButtonName";

		[Bindable]
		public function get mapButtonNameField():String {
			return _mapButtonNameField;
		}

		public function set mapButtonNameField(value:String):void {
			_mapButtonNameField=value;
		}

		/**
		 * Constructor
		 *
		 */
		public function IndonesiaProvinceMap() {
			super();
		}

		override protected function partAdded(partName:String, instance:Object):void {
			super.partAdded(partName, instance);
			if (instance is MapButton) {
				(instance as MapButton).addEventListener(MouseEvent.CLICK, mapButton_clickHandler);
					//				var renderer:ToolTipRenderer = toolTipRenderer.newInstance();
					//				renderer.data = (instance as MapButton).toolTipData;
					//				(instance as MapButton).toolTipRenderer = renderer;
			}
		}

		override protected function partRemoved(partName:String, instance:Object):void {
			super.partRemoved(partName, instance);
		}

		public function setMapData(mapButton:String, mapValue:Number, toolTipData:Object):void {
			if (this[mapButton] is MapButton) {
				(this[mapButton] as MapButton).mapValue=mapValue;
				(this[mapButton] as MapButton).toolTipData=toolTipData;
				var renderer:ToolTipRenderer=toolTipRenderer.newInstance();
				renderer.data=(this[mapButton] as MapButton).toolTipData;
				(this[mapButton] as MapButton).toolTipRenderer=renderer;
			}
		}

		private function mapButton_clickHandler(event:MouseEvent):void {
			selectedMap=event.currentTarget as MapButton;
			mapGroup.setElementIndex(selectedMap, mapGroup.numChildren - 1);
			if (selectedMap.selected) {
				dispatchEvent(new MapEvent(MapEvent.MAP_SELECTED));
			}
		}

		private function setAllData():void {
			for each (var mapDataObject:Object in mapData) {
				setMapData(mapDataObject[mapButtonNameField], mapDataObject[mapValueField], mapDataObject[toolTipDataField]);
			}
		}
	}
}
